package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author mr.wu
 * @Date 2025/1/5 11:06
 */
@Component
public class MessageListener {

    @RabbitListener(queues = "simple-queue")
    public void simpleQueue(String message) throws InterruptedException {
        System.out.println("消费者接收到的消息是：" + message);
        TimeUnit.MILLISECONDS.sleep(2);
    }

    @RabbitListener(queues = "work-queue")
    public void workQueue1(String message) throws InterruptedException {
        System.out.println("消费者接收到的消息是111：" + message);
        TimeUnit.MILLISECONDS.sleep(200);
    }

    @RabbitListener(queues = "simple-queue")
    public void workQueue2(String message) {
        System.out.println("消费者接收到的消息是222：" + message);
    }


    /**
     * 广播模型-消费者1
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout-queue1")
    public void fanoutListener1(String msg) {
        System.out.println("广播模型-消费者1-消费到消息：" + msg);
    }

    /**
     * 广播模型-消费者2
     *
     * @param msg
     */
    @RabbitListener(queues = "fanout-queue2")
    public void fanoutListener2(String msg) {
        System.out.println("广播模型-消费者2-消费到消息：" + msg);
    }

//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct-queue"),
//            exchange = @Exchange(name = "direct-exchange", type = ExchangeTypes.DIRECT),
//            key = {"red", "blue"}))


    /**
     * 路由key模型-消费者1
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct-queue1"),
            exchange = @Exchange(name = "direct-exchange", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}))
    public void directListener1(String msg) {
        System.out.println("direct-queue1-消费者-消费到消息：" + msg);
    }

    /**
     * 路由key模型-消费者2
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct-queue2"),
            exchange = @Exchange(name = "direct-exchange", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}))
    public void directListener2(String msg) {
        System.out.println("direct-queue2-消费者-消费到消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic-queue"),
            exchange = @Exchange(name = "topic-exchange", type = ExchangeTypes.TOPIC),
            key = {"chain.#"}
    ))
    public void topicListener(String msg) {
        System.out.println("topic-queue-消费者-消费到消息" + msg);
    }
}
